package com.isman.placeslist.adapter;

import java.util.ArrayList;

import com.isman.placeslist.apiobject.Status;
import com.isman.placeslist.ws.AsyncRunnerHeadler;
import com.isman.placeslist.ws.ServiceProvider;

import android.app.Activity;
import android.view.View;
import android.widget.ProgressBar;

public class StatusAdapterEvent {
	private Activity mActivity;
	private StatusAdapter mStatusAdapter;
	private ArrayList<Status> mStatusList;
	private ProgressBar pgbLoad = null;

	public StatusAdapterEvent(Activity mActivity,
			ArrayList<Status> mStatusList, ProgressBar pgbLoad) {
		this.mActivity = mActivity;
		this.mStatusList = mStatusList;
		this.pgbLoad = pgbLoad;
	}

	public void setAdapter(StatusAdapter mStatusAdapter) {
		this.mStatusAdapter = mStatusAdapter;
	}

	public void mStatusLike(final int index, final Status mStatus) {
		if (pgbLoad != null)
			pgbLoad.setVisibility(View.VISIBLE);
		ServiceProvider provider = new ServiceProvider(mActivity);
		provider.StatusLike(mStatus.getId(), new AsyncRunnerHeadler() {
			@Override
			public void onComplete(String json) {
				mStatus.setCanLike(false);
				mStatus.setLikeCount(mStatus.getLikeCount() + 1);
				mActivity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mStatusList.set(index, mStatus);
						mStatusAdapter.notifyDataSetChanged();
						if (pgbLoad != null)
							pgbLoad.setVisibility(View.GONE);
					}
				});
			}

			@Override
			public void onAuthenticate(String json) {
				if (pgbLoad != null)
					pgbLoad.setVisibility(View.GONE);
			}

			@Override
			public void onError(int statusCode, String json) {
				if (pgbLoad != null)
					pgbLoad.setVisibility(View.GONE);
			}

			@Override
			public void onConnectingError(String message) {
				if (pgbLoad != null)
					pgbLoad.setVisibility(View.GONE);
			}
		});
	}

	public void mStatusUnlike(final int index, final Status mStatus) {
		if (pgbLoad != null)
			pgbLoad.setVisibility(View.VISIBLE);
		ServiceProvider provider = new ServiceProvider(mActivity);
		provider.StatusUnlike(mStatus.getId(), new AsyncRunnerHeadler() {
			@Override
			public void onComplete(String json) {
				mStatus.setCanLike(true);
				mStatus.setLikeCount(mStatus.getLikeCount() - 1);
				mActivity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mStatusList.set(index, mStatus);
						mStatusAdapter.notifyDataSetChanged();
						if (pgbLoad != null)
							pgbLoad.setVisibility(View.GONE);
					}
				});
			}

			@Override
			public void onAuthenticate(String json) {
				if (pgbLoad != null)
					pgbLoad.setVisibility(View.GONE);
			}

			@Override
			public void onError(int statusCode, String json) {
				if (pgbLoad != null)
					pgbLoad.setVisibility(View.GONE);
			}

			@Override
			public void onConnectingError(String message) {
				if (pgbLoad != null)
					pgbLoad.setVisibility(View.GONE);
			}
		});
	}
}